package com.example.controller;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.common.result.Result;
import com.example.entity.dto.KeepPlanDto;
import com.example.entity.dto.KeepPlanPageDto;
import com.example.entity.matirx.KeepContent;
import com.example.entity.matirx.KeepPlan;
import com.example.exception.BaseException;
import com.example.exception.PageEmptyException;
import com.example.exception.SaveFailException;
import com.example.service.KeepContentService;
import com.example.service.KeepPlanService;
import com.github.pagehelper.Page;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/keepplan")
@Api(tags = "保养计划相关",position = 999)
@Slf4j
@DS("mes")
public class KeepPlanController {
    @Autowired
    private KeepPlanService keepPlanService;
    @Autowired
    private KeepContentService keepContentService;

    @PostMapping("/save")
    @ApiOperation("添加保养计划")
    @Transactional
    public Result saveKeepPlan(@RequestBody KeepPlanDto keepPlanDto) {
        boolean save = keepPlanService.save(keepPlanDto);
        if (!save) {
            throw new SaveFailException("保存保养计划时失败！");
        }
        List<KeepContent> keepContentList = keepPlanDto.getKeepContentList();
        keepContentList.iterator().forEachRemaining(keepContent -> {
            keepContent.setPlanId(keepPlanDto.getId());
        });
        boolean b = keepContentService.saveBatch(keepPlanDto.getKeepContentList());
        if (!b) {
            throw new SaveFailException("保存保养计划项目时失败！");
        }
        return Result.success();
    }

    @PostMapping("/edit")
    @ApiOperation("更新保养计划")
    public Result editKeepPlan(@RequestBody KeepPlanDto keepPlanDto) {
        boolean b = keepPlanService.updateById(keepPlanDto);
        if (!b) {
            throw new SaveFailException("更新保养计划时失败！");
        }
       /* LambdaUpdateWrapper<KeepContent> contentWrapper = new LambdaUpdateWrapper<>();
        contentWrapper.eq(KeepContent::getPlanId, keepPlanDto.getId());
        boolean remove = keepContentService.remove(contentWrapper);
        if (!remove) {
            throw new SaveFailException("删除保养计划项时失败！");
        }
        boolean saveBatch = keepContentService.saveBatch(keepPlanDto.getKeepContentList());
        if (!saveBatch) {
            throw new SaveFailException("保存保养计划项目时失败！");
        }*/
        return Result.success();
    }

    @ApiOperation("计划分页")
    @GetMapping("/page")
    public Result<Page> pageKeepPlan( KeepPlanPageDto planPageDto) {
        Page planPage = keepPlanService.pageWithContent(planPageDto);
        long total = planPage.getTotal();
        if (total == 0) {
            throw new PageEmptyException("计划分页数据为空！");
        }
        return Result.success(planPage);
    }

    @ApiOperation("保养计划列表")
    @GetMapping("/list")
    public Result<List<KeepPlan>> ListPlan() {
        List<KeepPlan> list = keepPlanService.listuse();
        if (list == null || list.size() == 0) {
            throw new BaseException("空的保养计划列表");
        }
        return Result.success(list);
    }

    @ApiOperation("批量删除保养计划")
    @ApiImplicitParams(@ApiImplicitParam(name = "ids", value = "保养计划id可以多个", required = true, dataType = "int"))
    @DeleteMapping("/delete")
    public Result deletePlan(@RequestBody List<Integer> ids) {
        boolean b = keepPlanService.removeByIds(ids);
        if (!b) {
            throw new SaveFailException("删除保养计划时失败！");
        }
        ids.forEach(id->{
            LambdaQueryWrapper<KeepContent> contentWrapper=new LambdaQueryWrapper<>();
            contentWrapper.eq(KeepContent::getPlanId, id);
            boolean remove = keepContentService.remove(contentWrapper);
            if (!remove) {
                throw new SaveFailException("删除保养计划项时失败！");
            }
        });
        return Result.success();
    }

}
